What is object-keys?
The object-keys npm package is a utility that provides a shim for the Object.keys method, which returns an array of a given object's own enumerable property names. It is particularly useful for environments that do not support the native Object.keys method, such as older versions of JavaScript engines.
What are object-keys's main functionalities?
Shim for Object.keys
Provides a cross-browser implementation of Object.keys to retrieve the keys of an object.
var keys = require('object-keys');
var obj = { a: 1, b: 2, c: 3 };
console.log(keys(obj)); // ['a', 'b', 'c']
Other packages similar to object-keys
lodash.keys
Lodash is a popular utility library that includes a method called _.keys, which is similar to object-keys. It provides a more robust solution for getting the keys of an object, handling edge cases and supporting a wider range of environments, but it is part of a larger library which may be more than needed if only this functionality is required.
core-js
Core-js is a modular standard library for JavaScript, which includes polyfills for many ECMAScript features. It offers a polyfill for Object.keys, among many other features. It is more comprehensive than object-keys, but also larger in size, which might not be ideal for projects looking to minimize dependencies.
#object-keys
An Object.keys shim. Invoke its "shim" method to shim Object.keys if it is unavailable.
Most common usage:
var keys = Object.keys || require('object-keys');
Example
var keys = require('object-keys');
var assert = require('assert');
var obj = {
a: true,
b: true,
c: true
};
assert.deepEqual(keys(obj), ['a', 'b', 'c']);
var keys = require('object-keys');
var assert = require('assert');
delete Object.keys;
var shimmedKeys = keys.shim();
assert.equal(shimmedKeys, keys);
assert.deepEqual(Object.keys(obj), keys(obj));
var keys = require('object-keys');
var assert = require('assert');
var shimmedKeys = keys.shim();
assert.equal(shimmedKeys, Object.keys);
assert.deepEqual(Object.keys(obj), keys(obj));
Source
Implementation taken directly from es5-shim, with modifications, including from lodash.
Tests
Simply clone the repo, npm install
, and run npm test